package com.dp.springsourcecodeanalysis.handler;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.dp.springsourcecodeanalysis.dto.ResponseDTO;
import com.dp.springsourcecodeanalysis.enums.HttpEnum;
import com.dp.springsourcecodeanalysis.exceptions.AppException;
import com.dp.springsourcecodeanalysis.helper.ResponseHelper;

import lombok.extern.slf4j.Slf4j;

/**
 *
 * @author shala/沙拉
 * @date 2022/1/20 15:27
 */
@RestControllerAdvice
@Slf4j
public class AppExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResponseDTO<String> errorHandler(Exception e) {
        log.error("logic exception error:{}", e.toString());
        return ResponseHelper.error((int)HttpEnum.SERVER_ERR.getCode(), HttpEnum.SERVER_ERR.getText());
    }

    /**
     * 业务异常
     *
     * @param e
     *            e
     * @return ResponseDTO
     */
    @ExceptionHandler({AppException.class})
    @ResponseBody
    public ResponseDTO<Object> handleApiException(AppException e) {
        return ResponseHelper.error((int)e.getCode(), e.getMessage());
    }
}
